package com.xnfh.framework.utils

import android.content.Context
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import java.util.ArrayList

/**
 * @author ziheng
 * @Description:
 * @date 2022/12/18 16:59
 */
object ChoppedUtil {
    //第一个参数表示要切割的List集合，第二个参数表示分割后的集合大小
    fun <T> chopped(list: List<T>?, L: Int): List<List<T>> {
        val parts: MutableList<List<T>> = ArrayList()
        if (list == null) return parts
        val N = list.size
        var i = 0
        while (i < N) {
            parts.add(
                ArrayList(
                    list.subList(i, Math.min(N, i + L))
                )
            )
            i += L
        }
        return parts
    }

    /**
     *      动态改变字体颜色
     */
    fun changeTextColor(
        context: Context,
        content: String,
        begin: Int,
        over: Int,
        beginColor: Int,
        overColor: Int
    ): SpannableStringBuilder {
        //动态改变字体颜色
        val style = SpannableStringBuilder(content)
        style.setSpan(
            ForegroundColorSpan(context.resources.getColor(beginColor)),
            0,
            begin,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        style.setSpan(
            ForegroundColorSpan(context.resources.getColor(overColor)),
            begin,
            over,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        return style
    }
}